from django.contrib.auth import authenticate, login,logout
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib import messages
from .forms import RegionMonthlyAwardCount2023Form, RegionMonthlyAwardAmount2023Form, RegionProjectCountForm, \
    RegionAwardAmount2023Form
from .models import Region1MonthlyAwardAmount2023, Region2MonthlyAwardAmount2023, Region3MonthlyAwardAmount2023, \
    RegionMonthlyAwardAmount2023, Region1MonthlyAwardCount2023, Region2MonthlyAwardCount2023, Region3MonthlyAwardCount2023, \
    RegionMonthlyAwardCount2023, RegionProjectCount,RegionAwardAmount2023

def index(request):
    region1_amount_data = list(Region1MonthlyAwardAmount2023.objects.values('month', 'award_amount'))
    region2_amount_data = list(Region2MonthlyAwardAmount2023.objects.values('month', 'award_amount'))
    region3_amount_data = list(Region3MonthlyAwardAmount2023.objects.values('month', 'award_amount'))
    region1_count_data = list(Region1MonthlyAwardCount2023.objects.values('month', 'award_count'))
    region2_count_data = list(Region2MonthlyAwardCount2023.objects.values('month', 'award_count'))
    region3_count_data = list(Region3MonthlyAwardCount2023.objects.values('month', 'award_count'))
    region_monthly_amount_data = list(RegionMonthlyAwardAmount2023.objects.values('month','region_1_award_amount', 'region_2_award_amount', 'region_3_award_amount'))
    region_monthly_count_data = list(RegionMonthlyAwardCount2023.objects.values('month','region_1_award_count', 'region_2_award_count', 'region_3_award_count'))
    regionproject_count_data = list(RegionProjectCount.objects.values('region', 'project_count'))
    regionawardamount_data = list(RegionAwardAmount2023.objects.values('region', 'total_award_amount'))


    # regionproject_count_data_json = json.dumps(regionproject_count_data)
    context = {
        'region1_amount_data': region1_amount_data,
        'region2_amount_data': region2_amount_data,
        'region3_amount_data': region3_amount_data,
        'region1_count_data': region1_count_data,
        'region2_count_data': region2_count_data,
        'region3_count_data': region3_count_data,
        'region_monthly_count_data': region_monthly_count_data,
        'region_monthly_amount_data': region_monthly_amount_data,
        'regionproject_count_data': regionproject_count_data,
        'regionawardamount_data': regionawardamount_data,
    }

    return render(request, 'app1/index.html', context)

# 展示所有数据 reg1a
def list_data1a(request):
    data = Region1MonthlyAwardAmount2023.objects.all()
    return render(request, 'app1/reg1a/list_data.html', {'data': data})

# 新增数据
def create_data1a(request):
    if request.method == 'POST':
        month = request.POST['month']
        award_amount = request.POST['award_amount']
        Region1MonthlyAwardAmount2023.objects.create(month=month, award_amount=award_amount)
        return redirect('list_data1a')
    return render(request, 'app1/reg1a/create_data.html')
# 更新数据
def update_data1a(request, pk):
    data = get_object_or_404(Region1MonthlyAwardAmount2023, pk=pk)
    if request.method == 'POST':
        data.month = request.POST['month']
        data.award_amount = request.POST['award_amount']
        data.save()
        return redirect('list_data1a')
    return render(request, 'app1/reg1a/update_data.html', {'data': data})
# 删除数据
def delete_data1a(request, pk):
    data = get_object_or_404(Region1MonthlyAwardAmount2023, pk=pk)
    if request.method == 'POST':
        data.delete()
        return redirect('list_data1a')
    return render(request, 'app1/reg1a/delete_data.html', {'data': data})


# reg2a
def list_data2a(request):
    data = Region2MonthlyAwardAmount2023.objects.all()
    return render(request, 'app1/reg2a/list_data.html', {'data': data})
# 新增数据
def create_data2a(request):
    if request.method == 'POST':
        month = request.POST['month']
        award_amount = request.POST['award_amount']
        Region2MonthlyAwardAmount2023.objects.create(month=month, award_amount=award_amount)
        return redirect('list_data2a')
    return render(request, 'app1/reg2a/create_data.html')
def update_data2a(request, pk):
    data = get_object_or_404(Region2MonthlyAwardAmount2023, pk=pk)
    if request.method == 'POST':
        data.month = request.POST['month']
        data.award_amount = request.POST['award_amount']
        data.save()
        return redirect('list_data2a')
    return render(request, 'app1/reg2a/update_data.html', {'data': data})
# 删除数据
def delete_data2a(request, pk):
    data = get_object_or_404(Region2MonthlyAwardAmount2023, pk=pk)
    if request.method == 'POST':
        data.delete()
        return redirect('list_data2a')
    return render(request, 'app1/reg2a/delete_data.html', {'data': data})



#创建reg3a
def list_data3a(request):
    data = Region3MonthlyAwardAmount2023.objects.all()
    return render(request, 'app1/reg3a/list_data.html', {'data': data})
# 新增数据
def create_data3a(request):
    if request.method == 'POST':
        month = request.POST['month']
        award_amount = request.POST['award_amount']
        Region3MonthlyAwardAmount2023.objects.create(month=month, award_amount=award_amount)
        return redirect('list_data3a')
    return render(request, 'app1/reg3a/create_data.html')
# 更新数据
def update_data3a(request, pk):
    data = get_object_or_404(Region3MonthlyAwardAmount2023, pk=pk)
    if request.method == 'POST':
        data.month = request.POST['month']
        data.award_amount = request.POST['award_amount']
        data.save()
        return redirect('list_data3a')
    return render(request, 'app1/reg3a/update_data.html', {'data': data})
# 删除数据
def delete_data3a(request, pk):
    data = get_object_or_404(Region3MonthlyAwardAmount2023, pk=pk)
    if request.method == 'POST':
        data.delete()
        return redirect('list_data3a')
    return render(request, 'app1/reg3a/delete_data.html', {'data': data})




# 展示所有数据 reg1c
def list_data1c(request):
    data = Region1MonthlyAwardCount2023.objects.all()
    return render(request, 'app1/reg1c/list_data.html', {'data': data})
# 新增数据
def create_data1c(request):
    if request.method == 'POST':
        month = request.POST['month']
        award_count = request.POST['award_count']
        Region1MonthlyAwardCount2023.objects.create(month=month, award_count=award_count)
        return redirect('list_data1c')
    return render(request, 'app1/reg1c/create_data.html')
# 更新数据
def update_data1c(request, pk):
    data = get_object_or_404(Region1MonthlyAwardCount2023, pk=pk)
    if request.method == 'POST':
        data.month = request.POST['month']
        data.award_count = request.POST['award_count']
        data.save()
        return redirect('list_data1c')
    return render(request, 'app1/reg1c/update_data.html', {'data': data})
# 删除数据
def delete_data1c(request, pk):
    data = get_object_or_404(Region1MonthlyAwardCount2023, pk=pk)
    if request.method == 'POST':
        data.delete()
        return redirect('list_data1c')
    return render(request, 'app1/reg1c/delete_data.html', {'data': data})

# reg2a
def list_data2c(request):
    data = Region2MonthlyAwardCount2023.objects.all()
    return render(request, 'app1/reg2c/list_data.html', {'data': data})
# 新增数据
def create_data2c(request):
    if request.method == 'POST':
        month = request.POST['month']
        award_count = request.POST['award_count']
        Region2MonthlyAwardCount2023.objects.create(month=month, award_count=award_count)
        return redirect('list_data2c')
    return render(request, 'app1/reg2c/create_data.html')

# reg3a
def update_data2c(request, pk):
    data = get_object_or_404(Region2MonthlyAwardCount2023, pk=pk)
    if request.method == 'POST':
        data.month = request.POST['month']
        data.award_count = request.POST['award_count']
        data.save()
        return redirect('list_data2c')
    return render(request, 'app1/reg2c/update_data.html', {'data': data})

# 删除数据
def delete_data2c(request, pk):
    data = get_object_or_404(Region2MonthlyAwardCount2023, pk=pk)
    if request.method == 'POST':
        data.delete()
        return redirect('list_data2c')
    return render(request, 'app1/reg2c/delete_data.html', {'data': data})



#创建reg3a
def list_data3c(request):
    data = Region3MonthlyAwardCount2023.objects.all()
    return render(request, 'app1/reg3c/list_data.html', {'data': data})

# 新增数据
def create_data3c(request):
    if request.method == 'POST':
        month = request.POST['month']
        award_count = request.POST['award_amount']
        Region3MonthlyAwardCount2023.objects.create(month=month, award_count=award_count)
        return redirect('list_data3c')
    return render(request, 'app1/reg3c/create_data.html')

# 更新数据
def update_data3c(request, pk):
    data = get_object_or_404(Region3MonthlyAwardCount2023, pk=pk)
    if request.method == 'POST':
        data.month = request.POST['month']
        data.award_count = request.POST['award_count']
        data.save()
        return redirect('list_data3c')
    return render(request, 'app1/reg3c/update_data.html', {'data': data})

# 删除数据
def delete_data3c(request, pk):
    data = get_object_or_404(Region3MonthlyAwardCount2023, pk=pk)
    if request.method == 'POST':
        data.delete()
        return redirect('list_data3c')
    return render(request, 'app1/reg3c/delete_data.html', {'data': data})

# reg12c
def list_data12c(request):
    data = RegionMonthlyAwardCount2023.objects.all()
    return render(request, 'app1/reg12c/list_data.html', {'data': data})
def create_data12c(request):
    if request.method == 'POST':
        form = RegionMonthlyAwardCount2023Form(request.POST)
        if form.is_valid():
            form.save()
            return redirect('list_data12c')
    else:
        form = RegionMonthlyAwardCount2023Form()
    return render(request, 'app1/reg12c/create_data.html', {'form': form})
def update_data12c(request, pk):
    data = get_object_or_404(RegionMonthlyAwardCount2023, pk=pk)
    if request.method == 'POST':
        form = RegionMonthlyAwardCount2023Form(request.POST, instance=data)
        if form.is_valid():
            form.save()
            return redirect('list_data12c')
    else:
        form = RegionMonthlyAwardCount2023Form(instance=data)
    return render(request, 'app1/reg12c/update_data.html', {'form': form})
def delete_data12c(request, pk):
    data = get_object_or_404(RegionMonthlyAwardCount2023, pk=pk)
    if request.method == 'POST':
        data.delete()
        return redirect('list_data12c')
    return render(request, 'app1/reg12c/delete_data.html', {'data': data})

# reg12a
def list_data12a(request):
    data = RegionMonthlyAwardAmount2023.objects.all()
    return render(request, 'app1/reg12a/list_data.html', {'data': data})
def create_data12a(request):
    if request.method == 'POST':
        form = RegionMonthlyAwardAmount2023Form(request.POST)
        if form.is_valid():
            form.save()
            return redirect('list_data12a')
    else:
        form = RegionMonthlyAwardAmount2023Form()
    return render(request, 'app1/reg12a/create_data.html', {'form': form})
def update_data12a(request, pk):
    data = get_object_or_404(RegionMonthlyAwardAmount2023, pk=pk)
    if request.method == 'POST':
        form = RegionMonthlyAwardAmount2023Form(request.POST, instance=data)
        if form.is_valid():
            form.save()
            return redirect('list_data12a')
    else:
        form = RegionMonthlyAwardAmount2023Form(instance=data)
    return render(request, 'app1/reg12a/update_data.html', {'form': form})
def delete_data12a(request, pk):
    data = get_object_or_404(RegionMonthlyAwardAmount2023, pk=pk)
    if request.method == 'POST':
        data.delete()
        return redirect('list_data12a')
    return render(request, 'app1/reg12a/delete_data.html', {'data': data})

# reg_all_c
def list_dataallc(request):
    data_list = RegionProjectCount.objects.all()
    return render(request, 'app1/reg_all_c/list.html', {'data_list': data_list})
def create_dataallc(request):
    if request.method == 'POST':
        form = RegionProjectCountForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('list_dataallc')
    else:
        form = RegionProjectCountForm()
    return render(request, 'app1/reg_all_c/create.html', {'form': form})
def update_dataallc(request, pk):
    data = get_object_or_404(RegionProjectCount, pk=pk)
    if request.method == 'POST':
        form = RegionProjectCountForm(request.POST, instance=data)
        if form.is_valid():
            form.save()
            return redirect('list_dataallc')
    else:
        form = RegionProjectCountForm(instance=data)
    return render(request, 'app1/reg_all_c/update.html', {'form': form})
def delete_dataallc(request, pk):
    data = get_object_or_404(RegionProjectCount, pk=pk)
    if request.method == 'POST':
        data.delete()
        return redirect('list_dataallc')
    return render(request, 'app1/reg_all_c/delete.html', {'data': data})


# reg_all_a
def list_dataalla(request):
    data_list = RegionAwardAmount2023.objects.all()
    return render(request, 'app1/reg_all_a/list.html', {'data_list': data_list})
def create_dataalla(request):
    if request.method == 'POST':
        form = RegionAwardAmount2023Form(request.POST)
        if form.is_valid():
            form.save()
            return redirect('list_dataalla')
    else:
        form = RegionAwardAmount2023Form()
    return render(request, 'app1/reg_all_a/create.html', {'form': form})
def update_dataalla(request, pk):
    data = get_object_or_404(RegionAwardAmount2023, pk=pk)
    if request.method == 'POST':
        form = RegionAwardAmount2023Form(request.POST, instance=data)
        if form.is_valid():
            form.save()
            return redirect('list_dataalla')
    else:
        form = RegionAwardAmount2023Form(instance=data)
    return render(request, 'app1/reg_all_a/update.html', {'form': form})
def delete_dataalla(request, pk):
    data = get_object_or_404(RegionAwardAmount2023, pk=pk)
    if request.method == 'POST':
        data.delete()
        return redirect('list_dataalla')
    return render(request, 'app1/reg_all_a/delete.html', {'data': data})



# 注册视图
def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('index')  # 注册成功后跳转到首页
    else:
        form = UserCreationForm()
    return render(request, 'app1/register.html', {'form': form})

# 登录视图
def user_login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('index')  # 登录成功后跳转到首页
        else:
            messages.error(request, '用户名或密码错误')
    return render(request, 'app1/login.html')

# 登出视图
def user_logout(request):
    logout(request)
    return redirect('index')  # 登出后跳转到首页